using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class VUnityEngineExtension
{
    public static Transform CoverChild(this Transform t, string name)
    {
        Transform ori = t.Find(name);
        if (ori != null) GameObject.DestroyImmediate(ori.gameObject);

        Transform child = new GameObject(name).transform;
        child.transform.SetParent(t);
        child.transform.localPosition = Vector3.zero;
        return child;
    }

    public static Transform UniqueChild(this Transform t, string name)
    {
        Transform ori = t.Find(name);
        if (ori != null) return ori;

        Transform child = new GameObject(name).transform;
        child.transform.SetParent(t);
        child.transform.localPosition = Vector3.zero;
        return child;
    }

    public static void RemoveChild(this Transform t, string name)
    {
        Transform child = null;
        while (FindChild())
        {
            GameObject.DestroyImmediate(child);
        };

        bool FindChild()
        {
            child = t.Find(name);
            if (child != null) return true;
            else return false;
        }
    }

    public static T UniqueComponent<T>(this GameObject gameObject) where T : Component
    {
        if (gameObject.TryGetComponent<T>(out var comp))
        {
            return comp;
        }
        else
        {
            return gameObject.AddComponent<T>();
        }
    }
    public static void RemoveComponent<T>(this GameObject gameObject) where T : Component
    {
        while (gameObject.TryGetComponent<T>(out var comp))
        {
            GameObject.DestroyImmediate(comp);
        };
    }

    public static GameObject GlobalUniqueGameObject(this string gameObjectName)
    {
        var go = GameObject.Find(gameObjectName);
        if (go == null)
            go = new GameObject(gameObjectName);
        return go;
    }

    public static void Dismiss<T>(this IList<T> list, Action<T> destroyAction)
    {
        if (list == null) return;
        for (int i = list.Count - 1; i >= 0; i--)
            destroyAction(list[i]);
        list = null;
    }
}



public static class VLLMExtension
{
    public static string ToLLMString(this IEnumerable lists, string head = null)
    {
        List<string> parts = new List<string>();
        foreach (var item in lists)
        {
            if (item != null)
                parts.Add(item.ToString());
        }

        string tolist = string.Join(", ", parts);
        return $"{head}: {{ {tolist} }}";
    }

    public static string ToLLMString(this IDictionary dictionary, string head = null)
    {
        List<string> parts = new List<string>();
        foreach (DictionaryEntry entry in dictionary)
        {
            if (entry.Key != null && entry.Value != null)
                parts.Add($"{entry.Key}: {entry.Value}");
        }

        string tolist = string.Join(", ", parts);
        return $"{head}: {{ {tolist} }}";
    }

    public static string ToKeysString(this IDictionary dictionary, string head = null)
    {
        List<string> keys = new List<string>();
        foreach (DictionaryEntry entry in dictionary)
        {
            if (entry.Key != null)
                keys.Add(entry.Key.ToString());
        }

        string keyList = string.Join(", ", keys);
        return $"{head}: [ {keyList} ]";
    }

}
